package ${webPackagePrefix}.${moduleName}.controller;

import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.context.UserContext;
import ${packagePrefix}.${moduleName}.entity.${entityClass};
import ${packagePrefix}.${moduleName}.service.I${entityClass}Service;
import ${packagePrefix}.${moduleName}.vo.${entityClass}VO;

import java.util.Date;
import java.util.List;

/**
 * ${entityDescr}控制器
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
@RequestMapping("/${moduleName}/${entityName}")
public class ${entityClass}Controller {

    @Autowired
    private I${entityClass}Service ${entityName}Service;

    @PostMapping("/add")
    public APIResult add(
        <#if utils.isNotEmpty(addFields)>
            <#list addFields as c>
            @ApiParam(name = "${c.name}", value = "${c.descr}", required = ${c.required}<#if utils.hasText(c.defaultValue)>, defaultValue = "${c.defaultValue}"</#if>)
            @RequestParam(name = "${c.name}", required = ${c.required}<#if utils.hasText(c.defaultValue)>, defaultValue = "${c.defaultValue}"</#if>) ${c.dataType} ${c.name},
            </#list>
        </#if>
    ) {
        ${entityClass} entity = new ${entityClass}();
        <#if utils.isNotEmpty(addFields)>
        <#list addFields as c>
        entity.set${utils.firstUpperCase(c.name)}(${c.name});
        </#list>
        </#if>
        entity.setCreatedId(UserContext.userId());
        entity.setCreatedName(UserContext.userName());
        entity.setCreatedTime(new Date());
        ${entityName}Service.save(entity);
        return APIResult.success();
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<String> ids) {
        ${entityName}Service.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(
     <#if utils.isNotEmpty(modifyFields)>
         <#list modifyFields as c>
            @ApiParam(name = "${c.name}", value = "${c.descr}", required = ${c.required}<#if utils.hasText(c.defaultValue)>, defaultValue = "${c.defaultValue}"</#if>)
            @RequestParam(name = "${c.name}", required = ${c.required}<#if utils.hasText(c.defaultValue)>, defaultValue = "${c.defaultValue}"</#if>) ${c.dataType} ${c.name},
         </#list>
     </#if>
    ) {
        ${entityClass} entity = new ${entityClass}();
        <#if utils.isNotEmpty(modifyFields)>
        <#list modifyFields as c>
        entity.set${utils.firstUpperCase(c.name)}(${c.name});
        </#list>
        </#if>
        entity.setModifiedId(UserContext.userId());
        entity.setModifiedName(UserContext.userName());
        entity.setModifiedTime(new Date());
        ${entityName}Service.updateById(entity);
        return APIResult.success();
    }

    @GetMapping("/{${entityName}Id}")
    public APIResult get(@PathVariable(value = "${entityName}Id") String ${entityName}Id) {
        return APIResult.success(${entityName}Service.getById(${entityName}Id));
    }

    @GetMapping("/page")
    public APIResult<QueryResult<${entityClass}>> page(
     <#if utils.isNotEmpty(conditionFields)>
         <#list conditionFields as c>
            @ApiParam(name = "${c.name}", value = "${c.descr}", required = false)
            @RequestParam(name = "${c.name}", required = false) ${c.dataType} ${c.name},
         </#list>
     </#if>
            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageNum", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        ${entityClass}VO vo = new ${entityClass}VO();
        <#if utils.isNotEmpty(conditionFields)>
            <#list conditionFields as c>
        vo.set${utils.firstUpperCase(c.name)}(${c.name});
            </#list>
        </#if>
        vo.setKeyword(keyword);
        vo.setOrderBy(orderBy);
        return APIResult.success(${entityName}Service.pageList(vo, pageNum, pageSize));
    }
}